package com.quanyan.common.encrypt;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author songzj
 * @date 16/4/18-15:20
 */
public class Coder {

    public static final String KEY_SHA = "SHA";
    public static final String KEY_MD5 = "MD5";
    public static final String KEY_RSA = "RSA";

    public static final String UTF_8 = "UTF-8";
    public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

    public static final String PUBLIC_KEY = "rsa_public.key";//公钥
    public static final String PRIVATE_KEY = "rsa_private.key";//私钥

    /**
     * BASE64解密
     *
     * @param content
     * @return
     */
    public static byte[] decryptBASE64(String content) throws IOException {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        return base64Decoder.decodeBuffer(content);
    }

    public static byte[] decryptBASE64(byte[] content) throws IOException {
        try {
            return decryptBASE64(new String(content, UTF_8));
        } catch (UnsupportedEncodingException e) {
        }
        return null;
    }

    /**
     * BASE64 加密
     *
     * @param content
     * @return
     */
    public static String encryptBASE64(byte[] content) {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encodeBuffer(content);
    }

    public static String encryptBASE64(String content) {
        try {
            return encryptBASE64(content.getBytes(UTF_8));
        } catch (UnsupportedEncodingException e) {
        }
        return null;
    }

    /**
     * MD5加密
     *
     * @param content
     * @return
     */
    public static byte[] encryptMD5(String content) throws IOException, NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
        md5.update(content.getBytes(UTF_8));
        return md5.digest();
    }

    /**
     * SHA 加密
     *
     * @param content
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static byte[] encryptSHA(String content) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
        sha.update(content.getBytes(UTF_8));
        return sha.digest();
    }

    /**
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getSalt() {
        try {
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            byte[] salt = new byte[16];
            sr.nextBytes(salt);
            return encryptBASE64(salt);
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }

    /**
     * URLEncoder编码
     * @param str
     * @return
     */
    public static String urlEncoder(String str){
        String encode = "";
        try {
            encode = URLEncoder.encode(str,UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encode;
    }

    /**
     * URLEncoder解码
     * @param str
     * @return
     */
    public static String urlDecoder(String str){
        String decode = "";
        try {
            decode = URLDecoder.decode(str, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return decode;
    }


}
